import { QUIView } from "./QUIView";
import QGamePlugin from "./QGamePlugin";

export type QUILabelConfig = {
    id?: string;
    class?: string;
    left?: number;
    top?: number;
    right?: number | undefined;
    bottom?: number | undefined;
    text?: string;
    width?: number;
    height?: number;
    widthRatio?: number;
    heightRatio?: number;

    fontSize?: number;
    fontColor?: string;
    fontFamily?: string;
};

export class QUILabel extends QUIView {
    protected name: string = "QUILabel";

    private text: string = "";
    private label: Phaser.GameObjects.Text;
    private fontSize: number = 16;
    private fontColor: string = "#ff0";
    private fontFamily: string = "Arial";

    get Text(): string {
        return this.text;
    }

    set Text(value: string) {
        this.text = value;
        this.label.setText(this.text);
        this.Width = this.label.width;
        this.Height = this.label.height;
    }

    set FontSize(value: number) {
        if (typeof value === "string") value = parseInt(value);
        this.fontSize = value;
        this.label.setFontSize(this.fontSize);
        this.Width = this.label.width;
        this.Height = this.label.height;
    }

    get FontSize(): number {
        return this.fontSize;
    }

    set FontColor(value: string) {
        this.fontColor = value;
        this.label.setColor(this.fontColor);
    }

    get FontColor(): string {
        return this.fontColor;
    }

    set FontFamily(value: string) {
        this.fontFamily = value;
        this.label.setFontFamily(this.fontFamily);
    }

    get FontFamily(): string {
        return this.fontFamily;
    }

    constructor(
        scene: Phaser.Scene,
        manage: QGamePlugin,
        config?: QUILabelConfig
    ) {
        super(scene, manage, config);

        this.label = scene.add.text(0, 0, "");
        this.label.setOrigin(0, 0);

        if (config) {
            if (config.text) {
                this.Text = config.text;
            }

            if (config.fontSize) {
                this.FontSize = config.fontSize;
            }
            if (config.fontColor) {
                this.FontColor = config.fontColor;
            }

            if (config.fontFamily) {
                this.FontFamily = config.fontFamily;
            }
        }
        this.alpha = 0;
        this.Container.add(this.label);
    }

    public destroy() {
        this.label.destroy(true);
        super.destroy();
    }

    public getViewAttribute() {
        return {
            ...super.getViewAttribute(),
            text: this.Text,
            fontSize: this.FontSize,
            fontColor: this.FontColor,
            fontFamily: this.FontFamily,
        };
    }
}
